import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

# 设置中文字体
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

# 读取数据
dish_df = pd.read_excel("餐饮连锁品牌数据_菜品信息4_cleaned.xlsx")

# 模拟销量数据
np.random.seed(42)
dish_df['销量'] = 800 - (dish_df['售价(元)'] * 6) + np.random.normal(0, 80, len(dish_df))
dish_df['销量'] = dish_df['销量'].clip(lower=30)

# 计算30-50元销量占比
total_sales = dish_df['销量'].sum()
target_sales = dish_df[(dish_df['售价(元)'] >= 30) & (dish_df['售价(元)'] <= 50)]['销量'].sum()
target_percent = (target_sales / total_sales) * 100

# 创建散点图
plt.figure(figsize=(12, 8))

# 绘制散点
colors = ['green' if p < 30 else 'blue' if p <= 50 else 'red' for p in dish_df['售价(元)']]
plt.scatter(dish_df['售价(元)'], dish_df['销量'], c=colors, alpha=0.7, s=50)

# 标注菜品名称
for i, row in dish_df.iterrows():
    plt.annotate(row['菜品名称'], (row['售价(元)'], row['销量']),
                xytext=(5, 5), textcoords='offset points', fontsize=6)

# 添加价格区间线
plt.axvline(x=30, color='red', linestyle='--', alpha=0.5)
plt.axvline(x=50, color='red', linestyle='--', alpha=0.5)

# 设置图表
plt.title('菜品售价 vs 销量分析')
plt.xlabel('售价 (元)')
plt.ylabel('销量 (份)')
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()

# 在控制台输出30-50元销量占比
print(f"30-50元区间销量占比: {target_percent:.1f}%")
print(f"目标占比: 60.0%")
print(f"差距: {60 - target_percent:.1f}个百分点")